<template>
	<view class="mapSearch-main">
		<map style="width: 100%; height: 100%;" ref="mapId" id="mapId" :latitude="mapParams.latitude" :longitude="mapParams.longitude" :markers="covers" @tap="openMap()"></map>
	</view>
</template>

<script>
	import amap from '@/libs/amap-wx.js'
	export default {
		data() {
			return {
				current: 0,
				mapParams: {
					latitude: 24.482709,
					longitude: 118.180997,
					name: "厦门市思明区",
					address: "厦门市思明区"
				},
				covers: [
					{
						id: 1,
				        latitude: 24.482709,//纬度，范围为-90~90，负数表示南纬
				        longitude: 118.180997,//经度，范围为-180~180，负数表示西经
						iconPath: '/static/images/marker-icon.png',
						width: 30
					}
				]
			};
		},
		onLoad() {
			const _this = this
			_this.hehe()
			_this.getData()
		},
		methods:{
			initData() {
				const _this = this
				uni.chooseLocation({ 
					latitude: _this.mapParams.latitude,
					longitude: _this.mapParams.longitude,
				    success: function (res) {
				        console.log('位置名称：' + res.name);
				        console.log('详细地址：' + res.address);
				        console.log('纬度：' + res.latitude);
				        console.log('经度：' + res.longitude);
						_this.mapParams.latitude = res.latitude
						_this.mapParams.longitude = res.longitude
						_this.mapParams.name = res.name
						_this.mapParams.address = res.address
						let covers = {
							id: 2,
							latitude: res.latitude,
							longitude: res.longitude,
							iconPath: '/static/images/marker-icon.png',
							width: 30
						}
						_this.covers = [covers]
				    }
				});
			},
			getData() {
				const _this = this
				uni.getLocation({
				    type: 'gcj02',
					geocode: true,
				    success: function (res) {
						console.log('=============：' + JSON.stringify(res))
						console.log('当前位置的经度：' + res.longitude)
				        console.log('当前位置的纬度：' + res.latitude)
						_this.mapParams.longitude = res.longitude
						_this.mapParams.latitude = res.latitude
						
						let covers = {
							id: 3,
							latitude: res.latitude,
							longitude: res.longitude,
							iconPath: '/static/images/marker-icon.png',
							width: 30
						}
						_this.covers = [covers]
						
						let address = res.address
						if(!address) {
							// let mapContext = uni.createMapContext("mapId");
							// let appMap = mapContext.$getAppMap();
							// console.log('appMap=======',mapContext)
							// appMap.reverseGeocode({latitude: res.latitude,longitude: res.longitude},function(res){
							// 	console.log('=====',res)
							// })
							return
						} 
						let location = `${address.country}${address.province}${address.city}${address.district}${address.street}${address.streetNum}`
						_this.mapParams.name = location
						_this.mapParams.address = location
						
				    },
					fail: function(err) {
						console.log('======',err)
					}
				});
			},
			openMap() {//点击地图
				// uni.openLocation(this.mapParams)
				this.initData()
			},
			async hehe() {
				let amapPlugin = new amap.AMapWX({
					key: '19e4ae6cadfe6dfba0396d08db5a84ae'  
				});
				amapPlugin.getRegeo({  
					success: (data) => {  
						console.log(JSON.stringify(data))
						
					}  
				});  
			}

		}

	}
</script>

<style lang="scss">
	@import "./mapSearch.scss";  
</style>
